<?php
/*
 * Aclass_group 用户组
 * 2012-05-08
 * abans@qq.com
 */
class Com_group{
	function  __construct($appid='') {
		$this->appid = '';
		if(!empty($appid)){
			$this->appid = $appid;
		}elseif(defined('Acan_app_id')){
			$this->appid = Acan_app_id;
		}
		$this->cname = 'Acan:group:';
		$this->_SS = Afun_session();
	}
	//--权限认证
	function auth(){
		$papp = $this->power_app();
		$pval = $papp->$_GET['mod']->$_GET['act'];
	//	print_r($papp);
		if($_GET && $_POST && $pval==3){
			return;
		}elseif($_POST && $pval>1){
			return;
		}elseif($_GET && count($_POST)==0 && $pval>0){
			return;
		}else{
			Afun_refresh("你没有该权限,不能执行此操作,请联系管理员!",'referrer');
			exit;
		}
	}
	//--返回应用的权限数据
	function power_app($mod='',$act=''){
		if(empty($mod) && empty($act)){
			$mod = $_GET['mod'];
			$act = $_GET['act'];
		}
		$pval = -1;
		$power = $this->power_get();
		if(empty($act)){
			if(isset($power->public->$mod)){
				$pval = $power->public;
			}elseif(isset($power->{Acan_app_name}->$mod)){
				$pval = $power->{Acan_app_name};
			}
		}else{
			if(isset($power->public->$mod->$act)){
				$pval = $power->public;
			}elseif(isset($power->{Acan_app_name}->$mod->$act)){
				$pval = $power->{Acan_app_name};
			}
		}
		return $pval;
	}
	//--判断模块的权限是否无权限
	function power_mod($mod){
		if(Afun_uid()==1){return 1;}
		$papp = $this->power_app($mod);
		if(is_object($papp) && isset($papp->$mod)){
			return array_sum(Afun_obj_arr($papp->$mod));
		}else{
			return -1;
		}
	}
	//--判断模块的控制权限，是否无权限
	function power_act($mod,$act){
		if(Afun_uid()==1){return 1;}
		$papp = $this->power_app($mod,$act);
		if(is_object($papp) && isset($papp->$mod->$act)){
			return $papp->$mod->$act;
		}else{
			return -1;
		}
	}
	//--根据用户组ID 保存权限对象数据
	function power_save($gid,$save,$admin=0){
		$group = Afun_db()->model(Acan_table_group);
		Afun_cache_rm($this->cname.'get:'.Acan_app_id.':'.$gid);
		$up = "data='".Ajson_encode($save)."'";
		if($admin==1){
			$up = "admin='".Ajson_encode($save)."'";
		}
		return $group->update($up,"gid=$gid and appid=".Acan_app_id);
	}
	//--根据用户组ID 获取权限对象数据
	function power_get($gid='',$admin=0){
		if(empty($gid)){$gid=$this->_SS['group'];}
		$cname = $this->cname.'get:'.Acan_app_id.':'.$gid;
		$data = Afun_cache_get($cname);
		if(empty($data)){
			$group = Afun_db()->model(Acan_table_group);
			$data = $group->find("gid=$gid and appid=".Acan_app_id);
			//--如果有继承则读取继承的权限
			if($data['pid']>0){
				$con['gid'] = $data['pid'];
				$con['appid'] = Acan_app_id;
				$data = $group->find($con);
			}
			Afun_cache_set($cname,$data,3600);
		}
		if($this->_SS['admin']==1 || $admin==1){
			if($data['admin']){
				$data['data'] = $data['admin'];
			}
		}
		return json_decode($data['data']);
	}
	function getgroup($uid){
		$mgroup = Afun_db()->model(Acan_table_user_group);
		$udata = $mgroup->ofind(array('uid'=>$uid,'appid'=>Acan_app_id));
		return $udata->group;
	}
	function get_group($uid){
		$mgroup = Afun_db()->model(Acan_table_user_group);
		return $mgroup->ofind(array('uid'=>$uid,'appid'=>Acan_app_id));
	}
	function getgroupadmin($uid){
		$mgroup = Afun_db()->model(Acan_table_user_group);
		$udata = $mgroup->ofind(array('uid'=>$uid,'appid'=>Acan_app_id));
		return $udata->admin;
	}
	//--用户组关联表acan_user_group信息删除
	function usergroupdel($uid){
		if(Afun_db()->model(Acan_table_user_group)->delete(array('uid'=>$uid,'appid'=>Acan_app_id))){
			return true;
		}else{
			return false;
		}
	}
	//--更新用户的用户组数据
	function usergroup_up($uid,$group,$admin=0){
		if($uid<1){return;}
		$mgroup = Afun_db()->model(Acan_table_user_group);
		$save = array('uid'=>$uid,'appid'=>Acan_app_id);
		$udata = $mgroup->ofind($save);
		if($udata->id){//--如果已经存在，则更新用户组ID
			if($group>0) $udata->group = $group;
			unset($udata->uid,$udata->appid);
			$save=$udata;
		}else{//--如果不存在，则添加用户组ID
			if($group>0) $save['group']=$group;
		}
		$udata->admin = $admin;
		return $mgroup->save($save);
	}
	function userdata(){
		$muser = Afun_db()->model(Acan_table_user);
		$mgroup = Afun_db()->model(Acan_table_user_group);
		$con->select = "a.uid,a.username,b.group";
		$con->where = "a.uid>1";
		$con->join = "LEFT JOIN `".Acan_table_user_group."` b ON a.uid=b.uid and b.appid=".Acan_app_id;
		return $muser->ofindAll($con);
	}
	function userlist(){
		$data = $this->userdata();
		foreach ($data as $k => $v) {
			$gobj[$v->uid] = $v->username;
		}
		return $gobj;
	}
	function usergrouplist(){
		$data = $this->userdata();
		foreach ($data as $k => $v) {
			$gobj[$v->group][$v->uid] = $v->username;
		}
		return $gobj;
	}
	//返回用户组枚举
	function enum(){
		$this->ckey = 'group_enum:'.$this->appid;
		$data = Afun_cache_get($this->ckey);
		if(empty($data)){
			$con->select = "a.gid,a.title";
			if(!empty($this->appid)){$con->where = "appid=".$this->appid;}
			$obj = Afun_db()->model(Acan_table_group,2)->findAll($con);
			foreach($obj as $k=>$v){
				$data[$v['gid']] = $v['title'];
			}
			Afun_cache_set($this->ckey,$data,3600);
		}
		return $data;
	}
	//获取应用的所有控制器的methods
	function getacts($app=''){
		if(empty($app)){
			$app = Acan_app_name;
		}
		$files = Afunc_file()->ascanfileinfo(A_ROOT.'app/'.$app.'/');
		foreach($files as $key=>$val){
			include_once($val['path']);
			$act = array_diff(get_class_methods($val['name']),get_class_methods('Acan'));
			$read = get_class_vars($val['name']);
			$modname = str_replace('Con_','',$val['name']);
			foreach($act as $k=>$v){
				$list[$modname]['title']=$read['Conread'];
				if(substr($v,0,4)!='Act_') continue;
				$actname = str_replace('Act_','',$v);
				$list[$modname]['act'][$actname]['title'] = $read['Actread'][$actname];
			}
		}
		return $list;
	}
	//清除缓存
	function delcache($gid=''){
		if(!empty($gid)){
			Afun_cache_rm($this->cname.'get:'.$gid);
		}
		Afun_cache_rm('group_enum:'.$this->appid);
	}
	function __help(){
		return array(
			'title'=>'用户组',
			'methods'=>array(
				'userdata'=>'',
				'userlist'=>'',
				'usergrouplist'=>'',
				'enum'=>'返回用户组枚举',
				'getacts'=>'获取应用的所有控制器的methods'
			)
		);
	}
}
?>
